package com.gqz.aircraftwar.mvp.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PointF
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.gqz.aircraftwar.util.CalcUtils
import com.gqz.aircraftwar.util.i
import kotlin.math.min

/**
 * 操纵杆
 *
 * @author gqz
 * @data 2020/3/25
 */

class Joystick : View, View.OnTouchListener {
    private val TAG: String = "Joystick-->"
    //larger circle
    var cx: Float = 0f
    var cy: Float = 0f
    var radius = 0f
    //smaller circle
    var cx2: Float = 0f
    var cy2: Float = 0f
    var radius2 = 0f

    var paint = Paint()
    var listener: OnJoystickChange? = null

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {
        paint.isAntiAlias = true
        paint.isDither = true
        setOnTouchListener(this)
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var wsize = MeasureSpec.getSize(widthMeasureSpec)
        var hsize = MeasureSpec.getSize(heightMeasureSpec)
        //使其为方形
        var size = min(wsize, hsize)
        setMeasuredDimension(size, size)
        cx = size.toFloat() / 2f
        cy = cx
        radius = size / 2f * 0.8f
        radius2 = size / 2 - radius
        cx2 = cx
        cy2 = cy
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        paint.color = Color.parseColor("#5f000000")
        canvas?.drawCircle(cx, cy, radius, paint)
        paint.color = Color.parseColor("#6fcccccc")
        canvas?.drawCircle(cx2, cy2, radius2, paint)

    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
            }
            MotionEvent.ACTION_MOVE -> {
                var startPos = PointF(cx, cy)
                var endPos = PointF(event.x, event.y)
                var distance = CalcUtils.distance(startPos, endPos)
                if (distance < radius) {
                    cx2 = event.x
                    cy2 = event.y
                } else {
                    var pos = CalcUtils.getPosOnLine(startPos, endPos, radius)
                    cx2 = pos.x
                    cy2 = pos.y
                }
                var speed = CalcUtils.calcSpeed(startPos, endPos)
//                num(TAG, "onTouch--> speedx:${speed[0]}, speedy:${speed[1]}")
                postInvalidate()
                listener?.onChange(speed[0], speed[1])
            }
            MotionEvent.ACTION_UP -> {
                resetC2()
                postInvalidate()
                listener?.onChange(0f, 0f)
            }
        }
        return true
    }

    fun resetC2() {
        cx2 = cx
        cy2 = cy
    }

    fun setOnJoystickChangeListener(listener: OnJoystickChange) {
        this.listener = listener
    }

    interface OnJoystickChange {
        fun onChange(speedx: Float, speedy: Float)
    }

}
